package 剑指offer;

import leetcode.editor.cn.MergeListNode;
import 抽象数据类型.ListNode;

/**
 * @description:
 * @author: ywk
 * @date: 2020-10-28
 */
public class 单链表的选择排序 {
    public static void main(String[] args) {
        ListNode l1 = null;
        l1 = MergeListNode.proList(new int[]{-3, -1, 2,9,0,1,3,-5}, 0, l1);
        sortInList(l1);
        MergeListNode.prtList(l1);
    }

    /**
     * @param head ListNode类 the head node
     * @return ListNode类
     */
    public static ListNode sortInList(ListNode head) {
        // write code here
        ListNode curNode = head;
        ListNode nextNode = curNode.next;
        while (curNode.next != null) {
            ListNode minNode = curNode;
            while ((nextNode != null)) {
                if (nextNode.val < minNode.val) {
                    minNode = nextNode;
                }
                nextNode = nextNode.next;
            }
            int temp = curNode.val;
            curNode.setVal(minNode.val);
            minNode.setVal(temp);
            curNode = curNode.next;
            nextNode = curNode.next;
        }
        return head;
    }
}
